%% runModelHelper_zf_fits_v1_8
%  Version 1.8
%  Author: Adeyinka Lesi
%  Date: 1/8/20
%  Project: Tumor Growth, Logarithmic Continuum Form
%% Version History
%  1.1: male and female parameters
%  1.2: playing with parameters
%  1.4: comparing multiple parameter sets
%  1.5: comparing varying exp parameters
%  1.6: new data
%  1.7: varying carrying capcity
%  1.8: test parameters
%  1.9: using varying weight thresholds
%  2.0: using runModelFunction_v5_0 - time zero sizes depth > 1
%  2.1: using rev2 data

startt = clock();

conv_int = getLinearConverter(1000);
conv_area = getLinearConverter(100);
conv_vol = getConverter();
select1 = 1:7;
select2 = [1:2 4:8];
select3 = [2:8 10:11 13:14];
% select3 = [2 4 6:7 9:11 13:14 16:17];

save_name = 'ct10_lt015_ht04_021720_rev2_hilo_shifted7_010621';
% 
% pfems = [
%     3.5546,0.79139,0,0,0.16877,0.00016877,0.47077,Inf,0,0
%     ];
% pmals = [
%     15.232,0.67368,0,0,7.8204e-05,7.8204e-08,0.99941,Inf,0,0
%     ];

%% kg and kr as of 8/13/20
% pfems = [
%     3.3111,0.78887,0,0,8.8984e-05,8.8984e-08,1,Inf,0,0
%     ];
% pmals = [
%     15.232,0.67368,0,0,7.8204e-05,7.8204e-08,0.99941,Inf,0,0
%     ];
% 
% pfems = [
% 3.3111,0.78887,1273,0.4407,8.8984e-05,8.8984e-08,1,Inf,4.035,0
% 3.3111,0.78887,1977.9,0.40886,8.8984e-05,8.8984e-08,1,Inf,4.085,0
% 3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
% 3.3111,0.78887,1096.7,0.45115,8.8984e-05,8.8984e-08,1,Inf,3.9911,0
% 3.3111,0.78887,979.96,0.45955,8.8984e-05,8.8984e-08,1,Inf,3.96,0
% 3.3111,0.78887,651.93,0.48894,8.8984e-05,8.8984e-08,1,Inf,3.8579,0
%     ];
% pmals = [
%     15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
% 15.232,0.67368,3.2261e+05,0.005803,7.8204e-05,7.8204e-08,0.99941,Inf,4.1697,0
% 15.232,0.67368,3.7249e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3571,0
% 15.232,0.67368,3.6334e+05,0.001266,7.8204e-05,7.8204e-08,0.99941,Inf,4.3239,0
% 15.232,0.67368,3.513e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.1553,0
% 15.232,0.67368,3.752e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.386,0
%     ];

% pfems = [
%     3.3111,0.78887,1273,0.4407,8.8984e-05,8.8984e-08,1,Inf,4.035,0
%     3.3111,0.78887,1977.9,0.40886,8.8984e-05,8.8984e-08,1,Inf,4.085,0
%     3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
%     3.3111,0.78887,1096.7,0.45115,8.8984e-05,8.8984e-08,1,Inf,3.9911,0
%     3.3111,0.78887,979.96,0.45955,8.8984e-05,8.8984e-08,1,Inf,3.96,0
%     3.3111,0.78887,651.93,0.48894,8.8984e-05,8.8984e-08,1,Inf,3.8579,0
%     3.3111,0.78887,171.07,0.60501,8.8984e-05,8.8984e-08,1,Inf,4.1273,0
%     3.3111,0.78887,174.16,0.60365,8.8984e-05,8.8984e-08,1,Inf,4.1423,0
%     3.3111,0.78887,190.33,0.59597,8.8984e-05,8.8984e-08,1,Inf,4.1347,0
%     3.3111,0.78887,188.53,0.59742,8.8984e-05,8.8984e-08,1,Inf,4.1269,0
%     ];
% pmals = [
%     15.232,0.67368,3.318e+05,0.00066567,7.8204e-05,7.8204e-08,0.99941,Inf,4.0502,0
%     15.232,0.67368,3.9335e+05,0.017832,7.8204e-05,7.8204e-08,0.99941,Inf,4.2776,0
%     15.232,0.67368,3.3455e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.0093,0
%     15.232,0.67368,3.3758e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.0293,0
%     15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
%     15.232,0.67368,3.2261e+05,0.005803,7.8204e-05,7.8204e-08,0.99941,Inf,4.1697,0
%     15.232,0.67368,3.7249e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3571,0
%     15.232,0.67368,3.6334e+05,0.001266,7.8204e-05,7.8204e-08,0.99941,Inf,4.3239,0
%     15.232,0.67368,3.513e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.1553,0
%     15.232,0.67368,3.752e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.386,0
%     ];


% %  082020 parameters including 5% high-low
% pfems = [
%     3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
%     3.340	0.790	1.271E+03	0.458	1.375E-04	1.375E-07	1.000	Inf	4.734   0
%     3.029	0.778	9.890E+02	0.438	8.623E-05	8.623E-08	0.998	Inf	3.961   0
%     ];
% pmals = [
%     15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
%     15.500	0.676	5.000E+05	0.022	8.546E-05	8.546E-08	1.000	Inf	4.400   0
%     4.393	0.537	3.556E+05	0.000	3.102E-05	3.102E-08	0.935	Inf	3.857   0
%     ];
pfems = [
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.340,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.029,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.790,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.778,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.78887,1.271E+03,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.78887,9.890E+02,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.458,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.438,8.8984e-05,8.8984e-08,1,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.45026,1.375E-04,1.375E-07,1,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.45026,8.623E-05,8.623E-08,1,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1.000,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,0.998,Inf,4.0875+3,0
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.734+3,0
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,3.961+3,0
    ];
pmals = [
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.500,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    4.393,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.676,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.537,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,5.000E+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.556E+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0.022,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0.000,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0,8.546E-05,8.546E-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0,3.102E-05,3.102E-08,0.99941,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,1.000,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.935,Inf,4.3256+3,0
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.400+3,0
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,3.857+3,0
    ];


mrs = cell(6,size(pfems,1));
run_list = [1:4];
if(~exist('objs_v1_10','var'))
    objs_v1_10 = zeros(6,size(pfems,1));
else
    if(size(pfems,1)>size(objs_v1_10,2))
        old_objs = objs_v1_10;
        objs_v1_10 = zeros(6,size(pfems,1));
        retain_list = setdiff(1:6,run_list);
        objs_v1_10(retain_list,1:size(old_objs,2)) = old_objs(retain_list,:);
    else
        objs_v1_10(run_list,1:size(pfems,1)) = 0;
    end
end

Nx = 500;
dt = 0.01;

min_surv = 2;
datafile1 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_RAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile2 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_NORAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile3 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_RAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
datafile4 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_NORAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
datafile5 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_NORAD_zf2019_f_area_vs_count_over_actprim.csv',min_surv);
datafile6 = sprintf('ct10_lt015_ht04_021720_rev2_s%i_081320/dist_NORAD_zf2019_m_area_vs_count_over_actprim.csv',min_surv);

stage_sel = 1;

for run_sel = run_list
    for i = 1:size(pfems,1)
        switch run_sel
            case 1
                pfem = pfems(i,:);
                params1 = pfem;
                params1([3:4 9:10]) = 0;
                [mrs{run_sel,i},data1,mg_fit1] = runModelFunction_v5_0(datafile1,params1,conv_vol,Nx,dt,select1,struct(),stage_sel);
                mrs{run_sel,i}.data = data1;
                mrs{run_sel,i}.fit = mg_fit1;
                axis([10 3e7 0 max(data1.cdf(end,:))]);
                objs_v1_10(1,i) = timeWeightedObjective_v1_0(mg_fit1,data1.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 2
                pfem = pfems(i,:);
                params2 = pfem;
                [mrs{run_sel,i},data2,mg_fit2] = runModelFunction_v5_0(datafile2,params2,conv_vol,Nx,dt,select2,struct(),3);
                mrs{run_sel,i}.data = data2;
                mrs{run_sel,i}.fit = mg_fit2;
                axis([10 3e7 0 max(data2.cdf(end,:))]);
                objs_v1_10(2,i) = timeWeightedObjective_v1_0(mg_fit2,data2.weights);
            case 3
                pmal = pmals(i,:);
                params3 = pmal;
                params3([3:4 9:10]) = 0;
                [mrs{run_sel,i},data3,mg_fit3] = runModelFunction_v5_0(datafile3,params3,conv_vol,Nx,dt,select1,struct(),stage_sel);
                mrs{run_sel,i}.data = data3;
                mrs{run_sel,i}.fit = mg_fit3;
                axis([10 3e7 0 max(data3.cdf(end,:))]);
                objs_v1_10(3,i) = timeWeightedObjective_v1_0(mg_fit3,data3.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 4
                pmal = pmals(i,:);
                params4 = pmal;
                [mrs{run_sel,i},data4,mg_fit4] = runModelFunction_v5_0(datafile4,params4,conv_vol,Nx,dt,select2,struct(),3);
                mrs{run_sel,i}.data = data4;
                mrs{run_sel,i}.fit = mg_fit4;
                axis([10 3e7 0 max(data4.cdf(end,:))]);
                objs_v1_10(4,i) = timeWeightedObjective_v1_0(mg_fit4,data4.weights);
            case 5
                pfem = pfems(i,:);
                params5 = pfem;
                [mrs{run_sel,i},data5,mg_fit5] = runModelFunction_v5_0(datafile5,params5,conv_vol,Nx,dt,select3,struct(),3);
                mrs{run_sel,i}.data = data5;
                mrs{run_sel,i}.fit = mg_fit5;
                axis([10 3e7 0 max(data5.cdf(end,:))]);
                objs_v1_10(5,i) = timeWeightedObjective_v1_0(mg_fit5,data5.weights);
            case 6
                pmal = pmals(i,:);
                params6 = pmal;
                [mrs{run_sel,i},data6,mg_fit6] = runModelFunction_v5_0(datafile6,params6,conv_vol,Nx,dt,select3,struct(),3);
                mrs{run_sel,i}.data = data6;
                mrs{run_sel,i}.fit = mg_fit6;
                axis([10 3e7 0 max(data6.cdf(end,:))]);
                objs_v1_10(6,i) = timeWeightedObjective_v1_0(mg_fit6,data6.weights);
        end
    end
end
save([save_name '_' mfilename],'mrs');

endt = clock();
duramins = etime(endt,startt)/60;

fprintf('%s took %3.2f minutes\n',mfilename,duramins);